/**
 * Oct 11, 2013 2:55:43 PM
 * com.kratonsolution.belian.security.impl.service
 * UserServiceRegistry.java
 */
package com.kratonsolution.belian.security.impl.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.kratonsolution.belian.kernel.service.AccessPermision;
import com.kratonsolution.belian.security.api.dao.AccessDao;
import com.kratonsolution.belian.security.api.dao.UserRoleDao;
import com.kratonsolution.belian.security.api.dm.Access;
import com.kratonsolution.belian.security.api.dm.UserRole;

/**
 * @author Agung Dodi Perdana
 * @email agung.dodi.perdana@gmail.com
 * @Version Sinch 0.0.1
 */
@Component
public class UserServiceRegistry
{
    private Logger log = Logger.getLogger(getClass());
    
    private final Map<String,Registry> REGISTRYS = new HashMap<String, Registry>();
    
    @Autowired
    private UserRoleDao userRoleDao;
    
    @Autowired
    private AccessDao accessDao;
    
    public UserServiceRegistry(){}
    
    public void init(String email)
    {
        log.info("Populating all security permission for user["+email+"]");
        
        if(!REGISTRYS.containsKey(email))
        {
            List<UserRole> roles = userRoleDao.loads(email);
            if(roles.isEmpty())
                throw new RuntimeException("Ilegal user access");
            
            Registry registry = new Registry();
            
            for(UserRole role:roles)
            {
                for(Access access:accessDao.loads(role.getRoleId()))
                {
                    registry.register(access);
                    log.info("Registering access for service["+access.getModuleName()+"]");
                }
            }
            
            REGISTRYS.put(email,registry);
        }
    }
    
    public void remove(String email)
    {
        REGISTRYS.remove(email);
    }
    
    public boolean hasAccess(String email,AccessPermision permission)
    {
        boolean hasaccess = false;
        if(!REGISTRYS.containsKey(email))
            return hasaccess;
        
        Registry registry = REGISTRYS.get(email);
        Access access = registry.get(permission.serviceCode());
        
        if(access == null)
            return hasaccess;
        
        switch (permission.type())
        {
            case NONE:
                hasaccess = true;
                break;
            case READ:
                if(access.isRead())
                    hasaccess = true;
                break; 
            case ADD:
                if(access.isAdd())
                    hasaccess = true;
                break;
            case UPDATE:
                if(access.isUpdate())
                    hasaccess = true;
                break;
            case DELETE:
                if(access.isDelete())
                    hasaccess = true;
                break;
            case PRINT:
                if(access.isPrint())
                    hasaccess = true;
                break;
        }
        
        return hasaccess;
    }
}
